﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LiteBlog.Models;
using LiteBlog.Components;
using LiteBlog.Components.Enums;
using LiteBlog.Controllers.Helpers;
using LiteBlog.Components.Contracts;
using LiteBlog.Common.Components;
using WURFL;
using LiteBlog.Common.Entities;

namespace LiteBlog.Controllers
{
    public class BaseController : Controller
    {
        private ISettingsComp _settingsComp;
        private ICacheComp _cacheComp;

        public ISettingsComp SettingsComponent
        {
            get
            {
                return _settingsComp;
            }
        }

        public ICacheComp CacheComponent
        {
            get
            {
                return _cacheComp;
            }
        }

        public UserModel UserModel { get; set; }

        protected Settings GetSettings()
        {
            return SettingsComponent.Load();
        }

        public BaseController()
        {
            UserModel = new UserModel(User);
        }

        public BaseController(ISettingsComp settingsComp, ICacheComp cacheComp)
        {
            UserModel = new UserModel(User);
            _settingsComp = settingsComp;
            _cacheComp = cacheComp;
        }

        public ActionResult PageNotFound()
        {
            return View("PageNotFound");
        }

        public ActionResult ErrorPage(Exception ex)
        {
            return View("Error", ex);
        }

        public ViewResult GetViewResult(string viewName, object model)
        {
            return View(viewName, model);
        }

        public JsonResult GetJsonResult(object model)
        {
            return Json(model);
        }

        protected virtual ResultModel GetResultModel(MethodResult result, params string[] values)
        {
            var resultModel = new ResultModel();
            string message = MessageHelper.GetMessage(GetType().Name, result);
            switch (result)
            {
                case MethodResult.CreateSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.DuplicateId:
                    resultModel.Result = false;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.DuplicateName:
                    resultModel.Result = false;
                    resultModel.Message = string.Format(message, values[1]);
                    break;
                case MethodResult.DuplicateEmail:
                    resultModel.Result = false;
                    resultModel.Message = string.Format(message, values[1]);
                    break;
                case MethodResult.Failure:
                    resultModel.Result = false;
                    resultModel.Message = message;
                    break;
                case MethodResult.UpdateSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.DeleteSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.PrimaryRecord:
                    resultModel.Result = false;
                    resultModel.Message = message;
                    break;
                case MethodResult.UnlockSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.PrimarySuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.EnableSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.DisableSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.LoginSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.LoginFailure:
                    resultModel.Result = false;
                    resultModel.Message = message;
                    break;
                case MethodResult.ChangePasswordSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.SaveSettingsSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.SaveSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.PublishSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.UnpublishSuccess:
                    resultModel.Result = true;
                    resultModel.Message = string.Format(message, values[0]);
                    break;
                case MethodResult.NotFound:
                    resultModel.Result = false;
                    resultModel.Message = message;
                    break;
                case MethodResult.Pending:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.MultipleDeleteSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.ApproveSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.RejectSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.MultipleApproveSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.MultipleRejectSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.BackupSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.GenerateSuccess:
                    resultModel.Result = true;
                    resultModel.Message = message;
                    break;
                case MethodResult.GenerateFailure:
                    resultModel.Result = false;
                    resultModel.Message = message;
                    break;
            }
            return resultModel;

        }

        protected string Skin { get; set; }

        protected string GetSiteLayout()
        {
            var layout = "_SiteLayout";
            if ((Request != null) && Request.Browser.IsMobileDevice)
            {
                var manager = SettingsComponent.GetWurflManager();
                int deviceWidth = DeviceHelper.GetDeviceWidth(manager, Request.UserAgent);
                if (deviceWidth <= TwoColumnLayoutWidth)
                    layout = "_TwoColumnSiteLayout";
                if (deviceWidth <= SingleColumnLayoutWidth)
                    layout = "_SingleColumnSiteLayout";
            }
            Logger.Debug(string.Format("Layout {0}", layout));
            return layout;
        }

        protected string GetAdminLayout()
        {
            if (Request != null && Request.Browser != null && Request.Browser.IsMobileDevice)
                return "_TabletAdminLayout";
            return "_AdminLayout";
        }

        private static readonly int SingleColumnLayoutWidth = 640;
        private static readonly int TwoColumnLayoutWidth = 1024;

    }
}
